const { sequelize } = require('../config/connect')
const {
  Video
} = require('../models/index') // 引入channel表
class VideoService {
  async addVideo(video) {
    return Video.create(video)
  }

  async getVideoById(id) {
    return Video.findOne({
      where: {
        id: id
      }
    })
  }

  async getVideoByName(name) {
    return Video.findOne({
      where: {
        name: name
      }
    })
  }

  async getNewVideo(offset, limit) {
    return Video.findAll({
      order: sequelize.col('createdAt', 'DESC'),
      offset: offset || 0,
      limit: limit || 10
    })
  }

  async getVideoByCategory(category, offset, limit) {
    return Video.findAll({
      where: {
        category: category
      },
      offset: +offset || 0,
      limit: +limit || 10
    })
  }
}

module.exports = new VideoService()